package com.graduate.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import com.graduate.model.SysRole;
import com.graduate.service.ISysRoleService;
import com.graduate.service.ISysUserRoleService;
import com.graduate.util.Message;
import com.graduate.util.Message.Status;
import com.graduate.util.PageInfo;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController{

	@Autowired
	private ISysRoleService roleService;
	@Autowired
	private ISysUserRoleService userAndRoleService;
	@RequestMapping("/index")
	public String index()
	{
		return "role/index";
	}
	
	@RequestMapping("/getPageInfo")
	@ResponseBody
	public PageInfo getPageInfo(@RequestParam Map<String, Object> map,
			@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows) {
		PageInfo pageInfo = new PageInfo(page, rows);
		pageInfo.setCondition(map);
		pageInfo = roleService.getPageInfo(pageInfo);
		return pageInfo;
	}
	
	/**
	 * 验证
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/validateRole", method = { RequestMethod.POST, RequestMethod.GET })
	public Boolean validateRole(@RequestParam Map<String, Object> map) {
		
		SysRole role1= roleService.selectRole(map);
			Boolean flag = true;
			if(null != role1){
				flag = false;
			}
			return flag;
	}
	
	/**
	 * @description 跳转到添加角色页面
	 * @return
	 */
	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(SysRole role,  Model model)
	{	
		if(role.getId()!=null && role.getId()!=""){
			SysRole role1=roleService.selectById(role.getId());	
			model.addAttribute("role", role1);
		}
		
		return "role/saveOrUpdate";
	}
	
	/**
	 * 
	 * @param role 需要查询的角色实体
	 * @return 返回角色的列表
	 */
	@RequestMapping(value = "/getRoleList", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public List<SysRole> getRoleList(SysRole role) {
		EntityWrapper<SysRole> wrapper = new EntityWrapper<SysRole>();
		
		wrapper.eq("status", null == role.getCode() ? "1" : role.getCode());
		List<SysRole> roleList = roleService.selectList(wrapper);
		return roleList;
	}
	
	/**
	 * @description 新增角色
	 * @param user
	 * @return
	 */
 
    @ResponseBody
    @RequestMapping(value = "/saveRole", method = { RequestMethod.POST, RequestMethod.GET }
    )
	public Message saveRole( @RequestBody SysRole role ) {
    	Message msg = new Message();
    	msg.setStatus(Status.SUCCESS);
			try {
				roleService.saveRole(role);
			} catch (Exception e) {
				msg.setStatus(Status.FAIL);
			}
			return msg;	      
	} 
    
    /**
	 * @description 修改角色
	 * @param user
	 * @return
	 */
    @ResponseBody
    @RequestMapping(value = "/toupdateRole", method = { RequestMethod.POST, RequestMethod.GET }
    )
	public Message toupdateRole( @RequestBody SysRole role ) {
    	Message msg = new Message();
    	msg.setStatus(Status.SUCCESS);
			try {
				roleService.updateRole(role);
				
			} catch (Exception e) {
				msg.setStatus(Status.FAIL);
			}
			return msg;	      
	}  
    
    /**
	 * @description 作废角色信息
	 * @param user
	 * @return
	 */
    @RequestMapping(value ="/delete", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Message delete(@RequestParam Map<String, Object> map)
	{
		Message msg = new Message();
		msg.setStatus(Status.SUCCESS);
		
		try {
			roleService.deleteRole((String)(map.get("id")));
		} catch (Exception e) {
			msg.setStatus(Status.FAIL);
		}
		return msg;
	}
    /**
     * @description 保存角色关联用户
     * @param 
     * @return
     */
    @RequestMapping(value ="/saveRoleAndUser", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public Message saveRoleAndUser(@RequestBody SysRole role)
    {
    	Message msg = new Message();
    	msg.setStatus(Status.SUCCESS);
    	try {
    		roleService.saveRoleAndUser(role);
    	} catch (Exception e) {
    		msg.setStatus(Status.FAIL);
    	}
    	return msg;
    }
    
    /**
     * 跳转到查看角色分配用户的表格页面
     */
    @RequestMapping(value ="/roleGrantUser", method = { RequestMethod.POST, RequestMethod.GET })
	public String roleToUser(Model model,String roleId,String name)
	{
    	model.addAttribute("roleId",roleId);
    	model.addAttribute("name",name);
		return "role/roleGrantUser";
	}
    
    /**
     * 当前角色分配用户的表格
     */
    @RequestMapping("/getPageInfoRoleGrantUser")
	@ResponseBody
	public PageInfo getPageInfoRoleGrantUser(@RequestParam Map<String, Object> map,
			@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows) {
		PageInfo pageInfo = new PageInfo(page, rows);
		pageInfo.setCondition(map);
		pageInfo = roleService.getPageInfoRoleGrantUser(pageInfo);
		return pageInfo;
	}
    
    /**
	 * @description 删除角色和用户中间表信息
	 * @param user
	 * @return
	 */
    @RequestMapping(value ="/deleteRoleGrantUser", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Message deleteRoleGrantUser(@RequestParam Map<String, Object> map)
	{
		Message msg = new Message();
		msg.setStatus(Status.SUCCESS);
		try {
			userAndRoleService.deleteById((String)(map.get("id")));
		} catch (Exception e) {
			msg.setStatus(Status.FAIL);
		}
		return msg;
	}
}
